/*
Filename:    sctp_one_to_many_connection.h
Author:      Erik Öjebo
Date:        2009-01-10
Description: Represents an SCTP one-to-many connection. Can be both a listening
             socket and an ordinary socket.

Notes:       Listening sockets are created by the static factory methods.
             To support dynamic binding/unbinding of addresses the connection
             must be created with
*/

#ifndef SCTP_ONE_TO_MANY_CONNECTION_H_
#define SCTP_ONE_TO_MANY_CONNECTION_H_

#include "address.h"
#include <netinet/sctp.h>
#include <pthread.h>
#include <vector>
#include "sctp_receive_result.h"
#include "association_collection.h"
#include "sctp_one_to_one_connection.h"
#include "sctp_connection.h"

class SctpOneToManyConnection : public SctpConnection
{
public:
    // Creates a new connection
    SctpOneToManyConnection();

    // Creates a listening connection, bound to the given address:
    // Note: If the socket is bound to the wildcard address (INADDR_ANY)
    //       dynamic binding/unbinding of addresses will not work.
    //       Instead, use createListeningConnection(int portNumber)
    //       to bind to all available addresses.
    static SctpOneToManyConnection createListeningConnection(
            const Address& address);

    // Creates a listening connection, bound to the given addresses.
    // Observe that all the addresses must have the same port number.
    static SctpOneToManyConnection createListeningConnection(
            const std::vector<Address>& addresses);

    // Creates a listening connection, bound to all available addresses:
    static SctpOneToManyConnection createListeningConnection(int portNumber);

    // Sends a message to a specific host.
    ssize_t sendToAddress(void const * const buffer, size_t size,
            const Address& address) const;

    // Sends a message to all active associations on the socket
    void sendToAll(void const * const buffer, size_t size) const;

    // Sets the time of inactivity an association is allowed before it is
    // closed.
    void setAutoCloseTime(int seconds);

    // Extracts an association from the one-to-many socket and creates a
    // new one-to-one connection for it.
    SctpOneToOneConnection peelOff(sctp_assoc_t associationId);

protected:
    // Creates and initializes a SCTP one-to-many socket.
    void createSocket();
};

#endif /* SCTP_ONE_TO_MANY_CONNECTION_H_ */

